/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editor;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorSite;

import ro.pub.cs.se.moondance.directedit.StatusLineValidationMessageHandler;
import ro.pub.cs.se.moondance.editpart.factory.UMLEditPartFactory;

/**
 * Functionality for configuring the GraphicalViewer
 */
public class GraphicalViewerCreator {

	private GraphicalViewer viewer;

	private IEditorSite editorSite;

	/**
	 * @param editorSite
	 */
	public GraphicalViewerCreator(IEditorSite editorSite) {
		this.editorSite = editorSite;
	}

	/**
	 * Creates a new <code>PaletteViewer</code>, configures, registers and
	 * initializes it.
	 * 
	 * @param parent
	 *            the parent composite
	 */
	public void createGraphicalViewer(Composite parent) {
		viewer = createViewer(parent);
	}

	/**
	 * @param parent
	 * @return
	 */
	protected GraphicalViewer createViewer(Composite parent) {

		StatusLineValidationMessageHandler validationMessageHandler = new StatusLineValidationMessageHandler(
				editorSite);
		GraphicalViewer viewer = new ValidationEnabledGraphicalViewer(
				validationMessageHandler);
		viewer.createControl(parent);

		// configure the viewer
		viewer.getControl().setBackground(ColorConstants.white);
		viewer.setRootEditPart(new ScalableFreeformRootEditPart());
		viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer));

		// initialize the viewer with input
		viewer.setEditPartFactory(getEditPartFactory());

		return viewer;

	}

	/**
	 * Returns the <code>EditPartFactory</code> that the
	 * <code>GraphicalViewer</code> will use.
	 * 
	 * @return the <code>EditPartFactory</code>
	 */
	protected EditPartFactory getEditPartFactory() {
		return new UMLEditPartFactory();
	}

	/**
	 * @return Returns the viewer.
	 */
	public GraphicalViewer getViewer() {
		return viewer;
	}

}